<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="prev" href="node320.html" />
<link rel="parent" href="pickle-protocol.html" />
<link rel="next" href="pickle-sub.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>13.1.5.3 Pickling and unpickling external objects</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="13.1.5.2 pickling and unpickling"
  href="node320.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="13.1.5 the pickle protocol"
  href="pickle-protocol.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="13.1.6 subclassing Unpicklers"
  href="pickle-sub.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node320.html">13.1.5.2 Pickling and unpickling</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="pickle-protocol.html">13.1.5 The pickle protocol</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="pickle-sub.html">13.1.6 Subclassing Unpicklers</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h3><a name="SECTION0015153000000000000000">
13.1.5.3 Pickling and unpickling external objects</a>
</h3>

<p>
For the benefit of object persistence, the <tt class="module">pickle</tt> module
supports the notion of a reference to an object outside the pickled
data stream.  Such objects are referenced by a ``persistent id'',
which is just an arbitrary string of printable ASCII characters.
The resolution of such names is not defined by the <tt class="module">pickle</tt>
module; it will delegate this resolution to user defined functions on
the pickler and unpickler.<a name="tex2html115"
  href="#foot36754"><sup>13.7</sup></a>
<p>
To define external persistent id resolution, you need to set the
<tt class="member">persistent_id</tt> attribute of the pickler object and the
<tt class="member">persistent_load</tt> attribute of the unpickler object.

<p>
To pickle objects that have an external persistent id, the pickler
must have a custom <tt class="function">persistent_id()</tt> method that takes an
object as an argument and returns either <code>None</code> or the persistent
id for that object.  When <code>None</code> is returned, the pickler simply
pickles the object as normal.  When a persistent id string is
returned, the pickler will pickle that string, along with a marker
so that the unpickler will recognize the string as a persistent id.

<p>
To unpickle external objects, the unpickler must have a custom
<tt class="function">persistent_load()</tt> function that takes a persistent id
string and returns the referenced object.

<p>
Here's a silly example that <em>might</em> shed more light:

<p>
<div class="verbatim"><pre>
import pickle
from cStringIO import StringIO

src = StringIO()
p = pickle.Pickler(src)

def persistent_id(obj):
    if hasattr(obj, 'x'):
        return 'the value %d' % obj.x
    else:
        return None

p.persistent_id = persistent_id

class Integer:
    def __init__(self, x):
        self.x = x
    def __str__(self):
        return 'My name is integer %d' % self.x

i = Integer(7)
print i
p.dump(i)

datastream = src.getvalue()
print repr(datastream)
dst = StringIO(datastream)

up = pickle.Unpickler(dst)

class FancyInteger(Integer):
    def __str__(self):
        return 'I am the integer %d' % self.x

def persistent_load(persid):
    if persid.startswith('the value '):
        value = int(persid.split()[2])
        return FancyInteger(value)
    else:
        raise pickle.UnpicklingError, 'Invalid persistent id'

up.persistent_load = persistent_load

j = up.load()
print j
</pre></div>

<p>
In the <tt class="module">cPickle</tt> module, the unpickler's
<tt class="member">persistent_load</tt> attribute can also be set to a Python
list, in which case, when the unpickler reaches a persistent id, the
persistent id string will simply be appended to this list.  This
functionality exists so that a pickle data stream can be ``sniffed''
for object references without actually instantiating all the objects
in a pickle.<a name="tex2html116"
  href="#foot36656"><sup>13.8</sup></a>  Setting
<tt class="member">persistent_load</tt> to a list is usually used in conjunction with
the <tt class="method">noload()</tt> method on the Unpickler.

<p>
<br><hr><h4>Footnotes</h4>
<dl>
<dt><a name="foot36754">... unpickler.</a><A
 HREF="node321.html#tex2html115"><sup>13.7</sup></a></dt>
<dd>The actual mechanism for
associating these user defined functions is slightly different for
<tt class="module">pickle</tt> and <tt class="module">cPickle</tt>.  The description given here
works the same for both implementations.  Users of the <tt class="module">pickle</tt>
module could also use subclassing to effect the same results,
overriding the <tt class="method">persistent_id()</tt> and <tt class="method">persistent_load()</tt>
methods in the derived classes.

</dd>
<dt><a name="foot36656">... pickle.</a><A
 HREF="node321.html#tex2html116"><sup>13.8</sup></a></dt>
<dd>We'll leave you with the image of Guido and Jim
sitting around sniffing pickles in their living rooms.

</dd>
</dl>
<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="13.1.5.2 pickling and unpickling"
  href="node320.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="13.1.5 the pickle protocol"
  href="pickle-protocol.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="13.1.6 subclassing Unpicklers"
  href="pickle-sub.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node320.html">13.1.5.2 Pickling and unpickling</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="pickle-protocol.html">13.1.5 The pickle protocol</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="pickle-sub.html">13.1.6 Subclassing Unpicklers</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
